<template>
  <div class="order-list">
    <div class="wrapper">
      <div class="container">
        <div class="order-box">
          <loading v-if="loading"></loading>
          <div class="order" v-for="(item,index) in orderList" :key="index">
            <div class="order-title">
              <div class="item-info fl">
                {{item.createTime}}
                <span>|</span>
                {{item.receiverName}}
                <span>|</span>
                订单号：{{item.orderNo}}
                <span>|</span>
                {{item.paymentTypeDesc}}
              </div>
              <div class="item-money fr">
                <span>应付金额：</span>
                <span class="money">{{item.payment}}</span>
                <span>元</span>
              </div>
            </div>
            <div class="order-content clearfix">
              <div class="good-box fl">
                <div class="good-list" v-for="(item,index) in item.orderItemVoList" :key="index">
                  <div class="good-img">
                    <img v-lazy="item.productImage" alt />
                  </div>
                  <div class="good-name">
                    <div class="p-name">{{item.productName}}</div>
                    <div class="p-money">{{item.currentUnitPrice}} X {{item.quantity}}元</div>
                  </div>
                </div>
              </div>
              <div class="good-state fr" v-if="item.statusDesc==20">
                <a href="javascript:;">{{item.statusDesc}}</a>
              </div>
              <div class="good-state fr" v-else>
                <a href="javascript:;" @click="goPay(item.orderNo)">{{item.statusDesc}}</a>
              </div>
            </div>
          </div>
          <!-- 当订单数据为0显示nodata -->
          <no-data v-if="!loading && orderList.length==0"></no-data>
          <!-- 使用按钮加载: -->
          <div class="load-more" v-show="showNextPage" v-if="false">
            <el-button type="primary" :loading="loading" @click="loadMore">加载更多</el-button>
          </div>
          <!-- 使用分页加载： -->
          <el-pagination
            class="pagination"
            background
            layout="prev, pager, next"
            :pageSize="pageSize"
            :total="total"
            @current-change="handleChange"
          ></el-pagination>
          <!-- 使用infiniteScroll无限滚动加载:  -->
          <div
            class="scroll-more"
            v-infinite-scroll="scrollMore"
            infinite-scroll-disabled="busy"
            infinite-scroll-distance="410"
            v-if="false"
          >
            <img src="/imgs/loading-svg/loading-spinning-bubbles.svg" alt v-show="loading" />
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script>
import Loading from "./../components/Loading";
import NoData from "./../components/Nodata";
import { Pagination, Button } from "element-ui";
import infiniteScroll from "vue-infinite-scroll";
export default {
  name: "orderList",
  components: {
    Loading,
    NoData,
    [Pagination.name]: Pagination,
    [Button.name]: Button
  },
  directives: {
    infiniteScroll
  },
  data() {
    return {
      loading: false,
      orderList: [], //订单列表
      pageSize: 5, //一次拉取条数
      pageNum: 1, //当前页码
      total: 0, //总条数
      showNextPage: true, //加载更多，是否显示按钮
      busy: false //滚动加载是否触发
    };
  },
  mounted() {
    this.getOrderList();
  },
  methods: {
    getOrderList() {
      this.loading = true;
      this.busy = true;
      this.axios
        .get("/orders/", {
          params: {
            pageNum: this.pageNum,
            pageSize: this.pageSize
          }
        })
        .then(res => {
          //  this.orderList=[] || res.list; //测试无数据情况
          // 普通加载数据:
          //  this.orderList=res.list;
          // 使用加载更多按钮点击的方式增加数据：
          this.orderList = this.orderList.concat(res.list);
          this.loading = false;
          this.total = res.total;
          this.showNextPage = res.hasNextPage;
          this.busy = false;
        })
        .catch(() => {
          //  出错的情况同样关闭loading
          this.loading = false;
        });
    },
    goPay(orderNo) {
      //  三种路由跳转方式：
      //  1.普通跳转：
      // this.router.push('/order/list')
      // 2.使用name传参(制定路由表里的路由名称)：
      // this.$router.push({
      //   name:'order-list',
      //   query:{
      //     orderNo
      //   }
      // })
      //  3.使用query传递参数，可以添加到地址栏：
      this.$router.push({
        path: "/order/pay",
        query: {
          orderNo
        }
      });
    },
    //  分页器：
    handleChange(pageNum) {
      window.console.log(pageNum);
      this.pageNum = pageNum;
      this.getOrderList();
    },
    //  点击加载更多：
    loadMore() {
      this.pageNum++;
      this.getOrderList();
    },
    // 无限滚动：
    scrollMore() {
      this.busy = true;
      setTimeout(() => {
        this.pageNum++;
        this.getScrolList();
      }, 500);
    },
    getScrolList() {
      this.loading = true;
      this.busy = true;
      this.axios
        .get("/orders/", {
          params: {
            pageNum: this.pageNum,
            pageSize: this.pageSize
          }
        })
        .then(res => {
          this.orderList = this.orderList.concat(res.list);
          this.loading = false;
          this.busy = false;
          if (res.hasNextPage) {
            this.busy = false;
          } else {
            this.busy = true;
          }
        })
        .catch(() => {
          this.loading = false;
        });
    }
  }
};
</script>
<style lang="less">
.order-list {
  .wrapper {
    background-color: @colorJ;
    padding-top: 33px;
    padding-bottom: 110px;

    .order-box {
      .pagination {
        text-align: right;
      }

      .order {
        .border();
        background-color: @colorG;
        margin-bottom: 31px;

        &:last-child {
          margin-bottom: 0;
        }

        .order-title {
          .height(74px);
          background-color: @colorK;
          padding: 0 43px;
          font-size: 16px;
          color: @colorC;

          .item-info {
            span {
              margin: 0 9px;
            }
          }

          .money {
            font-size: 26px;
            color: @colorB;
          }
        }

        .order-content {
          padding: 0 43px;

          .good-box {
            width: 88%;

            .good-list {
              display: flex;
              align-items: center;
              height: 145px;

              .good-img {
                width: 112px;

                img {
                  width: 100%;
                }
              }

              .good-name {
                font-size: 20px;
                color: @colorB;
              }
            }
          }

          .good-state {
            height: 145px;
            line-height: 145px;
            font-size: 20px;
            color: @colorA;

            a {
              color: @colorA;
            }
          }
        }
      }

      .pagination {
        text-align: right;
      }

      .el-pagination.is-background .el-pager li:not(.disabled).active {
        background-color: #ff6600;
      }

      .el-button--primary {
        background-color: #ff6600;
        border-color: #ff6600;
      }

      .load-more {
        text-align: center;
      }

      .load-more,
      .scroll-more {
        text-align: center;
      }
    }
  }
}
</style>